import { _decorator, Component, Node, Camera, Color, color, ProgressBar, Sprite, UIOpacity, v3, Vec3, Enum } from 'cc';
import { BasicComponet } from '../../Init/Basic/BasicComponet';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import GlobalPool from '../../Init/Tools/GlobalPool';
const { ccclass, property } = _decorator;

@ccclass('BossHpBar')
export class BossHpBar extends BasicComponet {
    data: { isShow: boolean, rate: number, isPlayer: boolean, wpos: Vec3, type: number } = null;
    markName = 'tankHpMark';

    @property({ type: Enum(GlobalEnum.HpBarType) })
    hpbarType: GlobalEnum.HpBarType = GlobalEnum.HpBarType.Tank;

    _camera: Camera = null;
    _offsetY = 0;
    setData(d: { data, camera: Camera, parent: Node }) {
        this.data = d.data;
        this._camera = d.camera;
        this.node.parent = d.parent;
        GlobalPool.putAllChildren(this.node);

        if (this.hpbarType == GlobalEnum.HpBarType.Tank) {
            this.markName = 'tankHpMark';
            this._offsetY = 120;
        } else {
            this.markName = 'armyHpMark';
            this._offsetY = 220;
        }

        for (let i = 0; i < this.data.rate; i++) {
            let bar = GlobalPool.get(this.markName);
            bar.children[0].active = false;
            bar.parent = this.node;
        }
        this.setHp();
        this.calPos();
    }

    reset() {
    }

    _tmpPos = v3();
    update(dt) {
        if (this.data && this.data.type != GlobalEnum.HpBarType.player) {
            //计算位置
            this.calPos();
            this.setHp();
            if (this.data.rate == 0) {
                GlobalPool.put(this.node);
            }
        }
    }

    setHp() {
        for (let i = 0; i < this.node.children.length; i++) {
            const e = this.node.children[i];
            e.children[0].active = !(i < this.data.rate);
        }
    }

    calPos() {
        this._camera.convertToUINode(this.data.wpos, this.node.parent, this._tmpPos);
        this._tmpPos.y += this._offsetY;
        this.node.setPosition(this._tmpPos);
    }
}

